package com.yogee.topseller.utils;

import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.adapter.TabLayoutAdapter;
import com.yogee.topseller.view.MyTabLayout;
import com.yogee.topseller.view.WrapContentHeightViewPager;

import java.lang.reflect.Field;
import java.util.List;

import static com.yogee.topseller.R.id.vp;

/**
 * @author hanyu
 *         created by 2017/6/23 11:34
 */

public class TabLayoutUtil {
    private TabLayout tab_FindFragment_title;                            //定义TabLayout
    private MyTabLayout tlProductType;
    private ViewPager vp_FindFragment_pager;                             //定义viewPager
    private FragmentPagerAdapter fAdapter;                               //定义adapter
    private List<Fragment> list_fragment;                                //定义要装fragment的列表
    private List<String> list_title;                                     //tab名称列表
    private FragmentManager mFm;
    private int distinguishLayout;                                             //区分布局变量0.首页，1.产品
    private String orderNum;

    private WrapContentHeightViewPager mViewPager;   // 自定义viewpager


    /**
     * @param fm
     * @param view      包含tablayout 和 viewpager的布局文件
     * @param fragments 内容展示的fragment
     * @param titles    标题
     */
    public TabLayoutUtil(FragmentManager fm, View view, List<Fragment> fragments, List<String> titles, int type) {
        mFm = fm;
        this.list_fragment = fragments;
        this.list_title = titles;
        this.distinguishLayout = type;
        init(view);
    }

    public TabLayoutUtil(FragmentManager fm, View view, List<Fragment> fragments, List<String> titles, int type,String types) {
        mFm = fm;
        this.list_fragment = fragments;
        this.list_title = titles;
        this.distinguishLayout = type;
        this.orderNum = types;
        init(view);
    }

    public void init(View view) {
        if (0 == distinguishLayout) {
//            tab_FindFragment_title = (TabLayout) view.findViewById(R.id.tl_product_type);
            tlProductType = (MyTabLayout) view.findViewById(R.id.tl_product_type);
            vp_FindFragment_pager = (ViewPager) view.findViewById(R.id.vp_product_list);
        } else if (1 == distinguishLayout) {
            tab_FindFragment_title = (TabLayout) view.findViewById(R.id.tl_product_type1);
            vp_FindFragment_pager = (ViewPager) view.findViewById(R.id.vp_product_list1);
        } else if (100 == distinguishLayout) {
            tab_FindFragment_title = (TabLayout) view.findViewById(R.id.collect_tab);
            vp_FindFragment_pager = (ViewPager) view.findViewById(R.id.collect_vp);
        } else if (101 == distinguishLayout) {
            tab_FindFragment_title = (TabLayout) view.findViewById(R.id.tab);
            vp_FindFragment_pager = (ViewPager) view.findViewById(vp);

        }else if(102== distinguishLayout){
            tab_FindFragment_title = (TabLayout) view.findViewById(R.id.tl_product_detail_new1);
            vp_FindFragment_pager = (ViewPager) view.findViewById(R.id.id_stickynavlayout_viewpager1);
//            mViewPager = (WrapContentHeightViewPager) view.findViewById(R.id.vp_product_detailinfo_new);
        }else if(110== distinguishLayout){
            tab_FindFragment_title = (TabLayout) view.findViewById(R.id.tl_gs);
            vp_FindFragment_pager = (ViewPager) view.findViewById(R.id.vp_gs);
//            mViewPager = (WrapContentHeightViewPager) view.findViewById(R.id.vp_product_detailinfo_new);
        }else if(111== distinguishLayout){
//            tab_FindFragment_title = (TabLayout) view.findViewById(R.id.tl_gs1);
//            mViewPager = (WrapContentHeightViewPager) view.findViewById(R.id.vp_gs1);
//            mViewPager = (WrapContentHeightViewPager) view.findViewById(R.id.vp_product_detailinfo_new);
        }else if(112== distinguishLayout){
            tab_FindFragment_title = (TabLayout) view.findViewById(R.id.tl_gs2);
            vp_FindFragment_pager = (ViewPager) view.findViewById(R.id.vp_gs2);
//            mViewPager = (WrapContentHeightViewPager) view.findViewById(R.id.vp_gs2);
        }
        //设置TabLayout的模式,两种模式
        //MODE_FIXED:固定tabs，并同时显示所有的tabs。
        //MODE_SCROLLABLE：可滚动tabs，显示一部分tabs，在这个模式下能包含长标签和大量的tabs，最好用于用户不需要直接比较tabs。
        if (100 == distinguishLayout || 101 == distinguishLayout || 102 == distinguishLayout|| 110 == distinguishLayout|| 112 == distinguishLayout  ||1==distinguishLayout) {
            tab_FindFragment_title.setTabMode(TabLayout.MODE_FIXED);
        } else {
            if(0== distinguishLayout){
                tlProductType.setTabMode(TabLayout.MODE_SCROLLABLE);
            }else{
                tab_FindFragment_title.setTabMode(TabLayout.MODE_SCROLLABLE);
            }
        }

        if(102!= distinguishLayout&&110!=distinguishLayout){
            if(distinguishLayout==0){
                fAdapter = new TabLayoutAdapter(mFm, list_fragment, list_title);

                //viewpager加载adapter
                vp_FindFragment_pager.setAdapter(fAdapter);
                //TabLayout加载viewpager
                tlProductType.setupWithViewPager(vp_FindFragment_pager);
                //为TabLayout添加tab名称
                for (int i = 0; i < list_fragment.size(); i++) {
                    TabLayout.Tab tab = tlProductType.getTabAt(i);//获得每一个tab
                    tab.setCustomView(R.layout.tab_item);//给每一个tab设置view
                    if (i == 0) {
                        // 设置第一个tab的TextView是被选择的样式
                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                    }
                    TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
                    textView.setText(list_title.get(i));//设置tab上的文字
                }
                tlProductType.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                        vp_FindFragment_pager.setCurrentItem(tab.getPosition());
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(14);//第一个tab被选中
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });

                //tab_FindFragment_title.set
                //设置TabLayout下端下划线宽度
                if (!TextUtils.isEmpty(orderNum)) {
                    switch (orderNum) {
                        case "0":
                            vp_FindFragment_pager.setCurrentItem(0);
                            break;
                        case "1":
                            vp_FindFragment_pager.setCurrentItem(1);
                            break;
                        case "2":
                            vp_FindFragment_pager.setCurrentItem(2);
                            break;
                        case "3":
                            vp_FindFragment_pager.setCurrentItem(3);
                            break;
                    }
                }

                tlProductType.post(new Runnable() {
                    @Override
                    public void run() {
                        if (100 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else if (101 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        }else if(0 == distinguishLayout){
//                            setIndicator1(tlProductType, 10, 10);
                        }else if(1 == distinguishLayout ){
                            setIndicator(tab_FindFragment_title, 0, 0);
                        }else if(102 == distinguishLayout||110==distinguishLayout ){
                            setIndicator(tab_FindFragment_title, 0, 0);
                        }
                    }
                });
            }else if(distinguishLayout == 1){
                fAdapter = new TabLayoutAdapter(mFm, list_fragment, list_title);

                //viewpager加载adapter
                vp_FindFragment_pager.setAdapter(fAdapter);
                //TabLayout加载viewpager
                tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
                //为TabLayout添加tab名称
                for (int i = 0; i < list_fragment.size(); i++) {
                    TabLayout.Tab tab = tab_FindFragment_title.getTabAt(i);//获得每一个tab
                    tab.setCustomView(R.layout.tab_item);//给每一个tab设置view
                    if (i == 0) {
                        // 设置第一个tab的TextView是被选择的样式
                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                    }
                    TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
                    textView.setText(list_title.get(i));//设置tab上的文字
                }
                tab_FindFragment_title.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                        vp_FindFragment_pager.setCurrentItem(tab.getPosition());
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(14);//第一个tab被选中
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });

                //tab_FindFragment_title.set
                //设置TabLayout下端下划线宽度
                if (!TextUtils.isEmpty(orderNum)) {
                    switch (orderNum) {
                        case "0":
                            vp_FindFragment_pager.setCurrentItem(0);
                            break;
                        case "1":
                            vp_FindFragment_pager.setCurrentItem(1);
                            break;
                        case "2":
                            vp_FindFragment_pager.setCurrentItem(2);
                            break;
                        case "3":
                            vp_FindFragment_pager.setCurrentItem(3);
                            break;
                    }
                }

                tab_FindFragment_title.post(new Runnable() {
                    @Override
                    public void run() {
                        if (100 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else if (101 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else {
                            setIndicator(tab_FindFragment_title, 17, 17);
                        }
                    }
                });

            }else if(distinguishLayout == 101){
                fAdapter = new TabLayoutAdapter(mFm, list_fragment, list_title);
                int[] imgRes = {R.mipmap.order_subscribe_black,R.mipmap.order_money_black,R.mipmap.order_contract_black,R.mipmap.order_commission_black};
                //viewpager加载adapter
                vp_FindFragment_pager.setAdapter(fAdapter);
                //TabLayout加载viewpager
                tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
                //为TabLayout添加tab名称
                for (int i = 0; i < list_fragment.size(); i++) {
                    TabLayout.Tab tab = tab_FindFragment_title.getTabAt(i);//获得每一个tab
                    tab.setCustomView(R.layout.tab_order_item);//给每一个tab设置view

                    TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
                    textView.setText(list_title.get(i));//设置tab上的文字
                    TextView tab_item_num_tv = (TextView) tab.getCustomView().findViewById(R.id.tab_item_num_tv);
                    ImageView img = (ImageView) tab.getCustomView().findViewById(R.id.tab_item_img);
                    img.setImageResource(imgRes[i]);
                }
                tab_FindFragment_title.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
//                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                        vp_FindFragment_pager.setCurrentItem(tab.getPosition());
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
//                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(14);//第一个tab被选中
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });

                //tab_FindFragment_title.set
                //设置TabLayout下端下划线宽度
                if (!TextUtils.isEmpty(orderNum)) {
                    switch (orderNum) {
                        case "0":
                            vp_FindFragment_pager.setCurrentItem(0);
                            break;
                        case "1":
                            vp_FindFragment_pager.setCurrentItem(1);
                            break;
                        case "2":
                            vp_FindFragment_pager.setCurrentItem(2);
                            break;
                        case "3":
                            vp_FindFragment_pager.setCurrentItem(3);
                            break;
                    }
                }
                tab_FindFragment_title.post(new Runnable() {
                    @Override
                    public void run() {
                        if (100 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else if (101 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else {
                            setIndicator(tab_FindFragment_title, 0, 0);
                        }
                    }
                });

            }else if(distinguishLayout == 111){
                fAdapter = new TabLayoutAdapter(mFm, list_fragment, list_title);
                int[] imgRes = {R.drawable.rong,R.drawable.bao,R.drawable.fa};
                //viewpager加载adapter
                mViewPager.setAdapter(fAdapter);
                //TabLayout加载viewpager
                tab_FindFragment_title.setupWithViewPager(mViewPager);
                //为TabLayout添加tab名称
                for (int i = 0; i < list_fragment.size(); i++) {
                    TabLayout.Tab tab = tab_FindFragment_title.getTabAt(i);//获得每一个tab
                    tab.setCustomView(R.layout.tab_gs_item);//给每一个tab设置view

                    TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
                    textView.setText(list_title.get(i));//设置tab上的文字
                    ImageView img = (ImageView) tab.getCustomView().findViewById(R.id.tab_item_img);
                    img.setImageResource(imgRes[i]);
                }
                tab_FindFragment_title.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
//                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                        mViewPager.setCurrentItem(tab.getPosition());

                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
//                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(14);//第一个tab被选中
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });

                //tab_FindFragment_title.set
                //设置TabLayout下端下划线宽度
                if (!TextUtils.isEmpty(orderNum)) {
                    switch (orderNum) {
                        case "0":
                            mViewPager.setCurrentItem(0);
                            break;
                        case "1":
                            mViewPager.setCurrentItem(1);
                            break;
                        case "2":
                            mViewPager.setCurrentItem(2);
                            break;
                        case "3":
                            mViewPager.setCurrentItem(3);
                            break;
                    }
                }
                tab_FindFragment_title.post(new Runnable() {
                    @Override
                    public void run() {
                        if (100 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else if (101 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else {
                            setIndicator(tab_FindFragment_title, 0, 0);
                        }
                    }
                });

            }else if(distinguishLayout == 112){


                fAdapter = new TabLayoutAdapter(mFm, list_fragment, list_title);

                //viewpager加载adapter
                vp_FindFragment_pager.setAdapter(fAdapter);
                //TabLayout加载viewpager
                tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
                //为TabLayout添加tab名称
                for (int i = 0; i < list_fragment.size(); i++) {
                    TabLayout.Tab tab = tab_FindFragment_title.getTabAt(i);//获得每一个tab
                tab.setCustomView(R.layout.tab_item1);//给每一个tab设置view
                    if (i == 0) {
                        // 设置第一个tab的TextView是被选择的样式
//                    ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                    }
                TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
                textView.setText(list_title.get(i));//设置tab上的文字
                }
                tab_FindFragment_title.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
//                    ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                        vp_FindFragment_pager.setCurrentItem(tab.getPosition());
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
//                    ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(14);//第一个tab被选中
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });

                //tab_FindFragment_title.set
                //设置TabLayout下端下划线宽度
                if (!TextUtils.isEmpty(orderNum)) {
                    switch (orderNum) {
                        case "0":
                            vp_FindFragment_pager.setCurrentItem(0);
                            break;
                        case "1":
                            vp_FindFragment_pager.setCurrentItem(1);
                            break;
                        case "2":
                            vp_FindFragment_pager.setCurrentItem(2);
                            break;
                        case "3":
                            vp_FindFragment_pager.setCurrentItem(3);
                            break;
                    }
                }

                tab_FindFragment_title.post(new Runnable() {
                    @Override
                    public void run() {
                        if (100 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else if (101 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        }else if (102 == distinguishLayout||110 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 0, 0);
                        } else {
                            setIndicator(tab_FindFragment_title, 0, 0);
                        }
                    }
                });

            }else{
                fAdapter = new TabLayoutAdapter(mFm, list_fragment, list_title);

                //viewpager加载adapter
                vp_FindFragment_pager.setAdapter(fAdapter);
                //TabLayout加载viewpager
                tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
                //为TabLayout添加tab名称
                for (int i = 0; i < list_fragment.size(); i++) {
                    TabLayout.Tab tab = tab_FindFragment_title.getTabAt(i);//获得每一个tab
//                    tab.setCustomView(R.layout.tab_item);//给每一个tab设置view
                    if (i == 0) {
                        // 设置第一个tab的TextView是被选择的样式
//                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                    }
//                    TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
//                    textView.setText(list_title.get(i));//设置tab上的文字
                }
                tab_FindFragment_title.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
//                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                        vp_FindFragment_pager.setCurrentItem(tab.getPosition());
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
//                        ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(14);//第一个tab被选中
                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });

                //tab_FindFragment_title.set
                //设置TabLayout下端下划线宽度
                if (!TextUtils.isEmpty(orderNum)) {
                    switch (orderNum) {
                        case "0":
                            vp_FindFragment_pager.setCurrentItem(0);
                            break;
                        case "1":
                            vp_FindFragment_pager.setCurrentItem(1);
                            break;
                        case "2":
                            vp_FindFragment_pager.setCurrentItem(2);
                            break;
                        case "3":
                            vp_FindFragment_pager.setCurrentItem(3);
                            break;
                    }
                }

                tab_FindFragment_title.post(new Runnable() {
                    @Override
                    public void run() {
                        if (100 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else if (101 == distinguishLayout) {
                            setIndicator(tab_FindFragment_title, 10, 10);
                        } else {
                            setIndicator(tab_FindFragment_title, 0, 0);
                        }
                    }
                });

            }

        // 102  || 110
        }else{

            fAdapter = new TabLayoutAdapter(mFm, list_fragment, list_title);

            //viewpager加载adapter
            vp_FindFragment_pager.setAdapter(fAdapter);
            //TabLayout加载viewpager
            tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
            //为TabLayout添加tab名称
            for (int i = 0; i < list_fragment.size(); i++) {
                TabLayout.Tab tab = tab_FindFragment_title.getTabAt(i);//获得每一个tab
//                tab.setCustomView(R.layout.tab_item);//给每一个tab设置view
                if (i == 0) {
                    // 设置第一个tab的TextView是被选择的样式
//                    ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                }
//                TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
//                textView.setText(list_title.get(i));//设置tab上的文字
            }
            tab_FindFragment_title.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
//                    ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(16);//第一个tab被选中
                    vp_FindFragment_pager.setCurrentItem(tab.getPosition());
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
//                    ((TextView) tab.getCustomView().findViewById(R.id.tab_item_textview)).setTextSize(14);//第一个tab被选中
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });

            //tab_FindFragment_title.set
            //设置TabLayout下端下划线宽度
            if (!TextUtils.isEmpty(orderNum)) {
                switch (orderNum) {
                    case "0":
                        vp_FindFragment_pager.setCurrentItem(0);
                        break;
                    case "1":
                        vp_FindFragment_pager.setCurrentItem(1);
                        break;
                    case "2":
                        vp_FindFragment_pager.setCurrentItem(2);
                        break;
                    case "3":
                        vp_FindFragment_pager.setCurrentItem(3);
                        break;
                }
            }

            tab_FindFragment_title.post(new Runnable() {
                @Override
                public void run() {
                    if (100 == distinguishLayout) {
                        setIndicator(tab_FindFragment_title, 10, 10);
                    } else if (101 == distinguishLayout) {
                        setIndicator(tab_FindFragment_title, 10, 10);
                    }else if (102 == distinguishLayout||110 == distinguishLayout) {
                        setIndicator(tab_FindFragment_title, 0, 0);
                    } else {
                        setIndicator(tab_FindFragment_title, 10, 10);
                    }
                }
            });
        }


    }

    /**
     * 改变TabLayout底部下划线的宽度
     *
     * @param tabs
     * @param leftDip
     * @param rightDip
     */
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    /**
     * 改变TabLayout底部下划线的宽度
     *
     * @param tabs
     * @param leftDip
     * @param rightDip
     */
    public void setIndicator1(MyTabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

}
